<?php

class AsyncController extends Zend_Controller_Action
{

    protected $_sesssion = null;

    public function init()
    {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        $this->_sesssion = new Application_Model_Session();
        
    }

    public function indexAction()
    {
        // action body
    }

    public function getpegawaiAction()
    {
        $namespace = 'pegawai';
        
        $nik = $this->getRequest()->getParam('nik');
        
        if($this->_sesssion->checkSession($nik, $namespace)){
            $result = $this->getPegawaiNik($nik);
            
            echo Zend_Json_Encoder::encode($result);
            if($result){
                $data = array(
                    'id_namespace' => $nik,
                    'namespace' => $namespace
                );

                $this->_sesssion->insertSession($data);
            }
            
        } else {
            return false;
        }
    }
    
    public function deletepegawaiAction()
    {
        $namespace = 'pegawai';
        $id = $this->getRequest()->getParam('id');
        $id_project = $this->getRequest()->getParam('id_project');

        if(!$this->_sesssion->checkSession($id, $namespace)){
            $this->_sesssion->deleteSessionById($id);
        } else {
            $anggota = new Application_Model_Pegawai();
            
            $data = array('status_pekerjaan' => '0');
            $anggota->pegawaiUpdate($data, $id);
            
            $pekerja = new Application_Model_Pekerja();
            $pekerja->pekerjaDelete($id, $id_project);
        }
    }
    
    public function deletegensetAction()
    {
        $namespace = 'genset';
        $id = $this->getRequest()->getParam('id');

        if(!$this->_sesssion->checkSession($id, $namespace)){
            $this->_sesssion->deleteSessionById($id);
        } else {
            $genset = new Application_Model_Genset();
            
            $data = array('status_genset' => 'Belum Terpakai','id_proyek' => null);
            
            $genset->gensetUpdate($data, $id);
        }
    }


    public function getnameAction()
    {
        $nik = $this->_request->getPost('nik');
        $anggota = new Application_Model_Pegawai();
        $result = $anggota->getPegawaiNik($nik);
        
        if($result){
            if($result['status_pekerjaan'] != 1)
                echo $result['nama_pegawai'];
            else
                echo 'Employee Already in Project';
        }else{
            echo 'Employee Not Found';
        }
    }

    public function getkonsumenAction()
    {
        $namespace = 'konsumen';
        
        $id_konsumen = $this->getRequest()->getParam('id_konsumen');
        $konsumen = new Application_Model_Konsumen();
        
        $session = $this->_sesssion->checkNamespaceSession($namespace);
        
        if($session)
        {
            $data = array( 'id_namespace' => $id_konsumen);
            $id = 0;
            foreach($session as $konsumen)
            {
                $id = $konsumen->id;
            }
            
            $this->_sesssion->updateSession($data, $id);
            
            $konsumen = new Application_Model_Konsumen();
            $result = $konsumen->getDatabyId($id_konsumen);
            
            echo Zend_Json_Encoder::encode($result);
        }
        elseif($this->_sesssion->checkSession($id_konsumen, $namespace)){
            $result = $konsumen->getDatabyId($id_konsumen);
            echo Zend_Json_Encoder::encode($result);
            
            if($result){
                $data = array(
                    'id_namespace' => $id_konsumen,
                    'namespace' => $namespace
                );

                $this->_sesssion->insertSession($data);
            }
        } else {
            return false;
        }
    }

    public function getgensetAction()
    {
        $namespace = 'genset';
        
        $id_genset = $this->getRequest()->getParam('id_genset');
        $genset = new Application_Model_Genset();
        
        if($this->_sesssion->checkSession($id_genset, $namespace) and $genset->getGensetBelumTerpakai($id_genset)){
            $result = $genset->getDataJoinId($id_genset);
        
            echo Zend_Json_Encoder::encode($result);
            
            if($result){
                $data = array(
                    'id_namespace' => $id_genset,
                    'namespace' => $namespace
                );

                $this->_sesssion->insertSession($data);
            }
            
        } else {
            $data = array('response' => 'Genset Already Used');
            echo Zend_Json_Encoder::encode($data);
            return false;
        }
    }

    private function getPegawaiNik($nik)
    {
        $anggota = new Application_Model_Pegawai();
        $result = $anggota->getPegawaiKosong($nik);
        return $result;
    }

    public function getdayaAction()
    {
        $daya = $this->getRequest()->getParam('daya');
        if($daya) {
            $harga = new Application_Model_HargaSewa();
            $tanpaBBM = $harga->getRow($daya, 'Tanpa BBM');
            $denganBBM = $harga->getRow($daya, 'Dengan BBM');
            $cols = $harga->info(Zend_Db_Table_Abstract::COLS);

            $coba = array();
            foreach($cols as $cols)
            {
                $coba[] = $cols;
            }

            $data = array();

            $i = 0;
            
            foreach($tanpaBBM as $tanpaBBM)
            {
                $cols = $coba[$i];
                $data[$cols] = $tanpaBBM;
                $i++;
            }

            $i = 0;
            foreach($denganBBM as $denganBBM)
            {
                $cols = $coba[$i];
                $data[$cols.'_bbm'] = $denganBBM;
                $i++;
            }

            echo Zend_Json_Encoder::encode($data);
        } else {
            echo '';
        }
    }
}









